package fr.jade.console.cmd;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.objectweb.fractal.api.Component;

import fr.jade.console.core.AbstractCommand;
import fr.jade.console.core.Command;
import fr.jade.console.core.Environment;

public class Call extends AbstractCommand implements Command {

  public String getDescription(){
    return "calls a method on a component";
  }
  
  public String getHelp(){
    return "call <itfName> <methodName> <param,..,param>";
  }
  /**
   * invoke a method.
   * 
   * @param pargs
   *          args[0] = cmdName
   *          args[1] = itfName
   *          args[2] = methodName
   *          args[3] = param 
   */

  public Object execute(String[] args, Environment env) throws Throwable {
    Component curent = (Component)env.getEnvVar("curent");
    Object itf = curent.getFcInterface(args[1]);
    Method m = itf.getClass().getMethod(args[2],String.class);
    try{
      m.invoke(itf,new Object[]{args[3]});
    }catch(InvocationTargetException e){
      throw e.getTargetException();
    }
    return null;
  }

  public String getName() {
    return "call";
  }
}
